package com.newland.mesdk.demo.interfaceimpl;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

import android.graphics.Bitmap;

import com.newland.mtype.DeviceRTException;
import com.newland.mtype.ModuleType;
import com.newland.mtype.module.common.printer.FontSettingScope;
import com.newland.mtype.module.common.printer.FontType;
import com.newland.mtype.module.common.printer.LiteralType;
import com.newland.mtype.module.common.printer.PrintContext;
import com.newland.mtype.module.common.printer.Printer;
import com.newland.mtype.module.common.printer.PrinterResult;
import com.newland.mtype.module.common.printer.PrinterStatus;
import com.newland.mtype.module.common.printer.ThrowType;
import com.newland.mtype.module.common.printer.WordStockType;
import com.newland.mesdk.demo.moduleinterface.PrinterInterface;
import com.newland.mesdk.demo.util.ModuleBase;

/**
 * Created by HJP 打印模块接口实现.
 */
public class PrinterInterfaceImpl extends ModuleBase implements PrinterInterface {
	private Printer printer;

	public PrinterInterfaceImpl() {
		printer = (Printer) factory.getModule(ModuleType.COMMON_PRINTER);
	}

	public PrinterResult checkThenPrint(PrintContext printContext, byte[] data, long timeout, TimeUnit timeunit) {
		return null;
	}

	@Override
	public PrinterStatus getStatus() {
		init();
		return printer.getStatus();
	}

	@Override
	public void init() {
		printer.init();
	}

	@Override
	public void paperThrow(ThrowType type, int distance) {
		printer.paperThrow(type, distance);
	}

	// 打印图片
	@Override
	public void printBitMap(int position, Bitmap bitmap) {
		printer.init();
		printer.print(position, bitmap, 30, TimeUnit.SECONDS);
	}

	@Override
	public PrinterResult printString(String data) {
		printer.init();
		return printer.print(data, 30, TimeUnit.SECONDS);
	}

	// 打印脚本
	@Override
	public PrinterResult printScript(String data) {
		byte[] printData;
		init();
		try {
			printData = data.getBytes("GBK");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			throw new DeviceRTException(-1, "脚本执行失败!");
		}
		return printer.printByScript(PrintContext.defaultContext(), printData, 60, TimeUnit.SECONDS);
	}

	@Override
	public void setDensity(int value) {
		init();
		printer.setDensity(value);
	}

	@Override
	public void setFontType(LiteralType literalType, FontSettingScope settingScope, FontType fontType) {
		init();
		printer.setFontType(literalType, settingScope, fontType);
	}

	@Override
	public void setLineSpace(int value) {
		init();
		printer.setLineSpace(value);
	}

	@Override
	public void setWordStock(WordStockType type) {
		init();
		printer.setWordStock(type);
	}
}
